package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Log;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ConfirmOrderFormServlet extends HttpServlet {
    private static final String CONFIRM_ORDER_FORM = "/WEB-INF/jsp/order/ConfirmOrder.jsp";
    private static final String SHIPPING_FORM = "/WEB-INF/jsp/order/ShippingForm.jsp";

    private String shippingAddressRequired;
    private Order order;
    private OrderService orderService = new OrderService();
    private LogService logService = new LogService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        shippingAddressRequired = request.getParameter("shippingAddressRequired");
        order = new Order();

        HttpSession session = request.getSession();
        order = (Order)session.getAttribute("order");
        Account account = (Account)session.getAttribute("account");
        String billToFirstName = request.getParameter("billToFirstName");
        order.setBillToFirstName(billToFirstName);
        String billToLastName = request.getParameter("billToLastName");
        order.setBillToLastName(billToLastName);
        String address1 = request.getParameter("billAddress1");
        order.setBillAddress1(address1);
        String address2 = request.getParameter("billAddress2");
        order.setBillAddress2(address2);
        String city = request.getParameter("billCity");
        order.setBillCity(city);
        String state = request.getParameter("billState");
        order.setBillState(state);
        String zip = request.getParameter("billZip");
        order.setBillZip(zip);
        String country = request.getParameter("billCountry");
        order.setBillCountry(country);

//
//        order.setShipToFirstName(account.getFirstName());
//        order.setShipToLastName(account.getLastName());
//        order.setShipAddress1(account.getAddress1());
//        order.setShipAddress2(account.getAddress2());
//        order.setShipCity(account.getCity());
//        order.setShipState(account.getState());
//        order.setShipZip(account.getZip());
//        order.setShipCountry(account.getCountry());

        if (shippingAddressRequired == null){
            String logInfo = logService.logInfo(" ")  + " 确认终止订单 ";
            logService.insertLogInfo(account.getUsername(), logInfo);
            request.getRequestDispatcher(CONFIRM_ORDER_FORM).forward(request, response);
        }
        else{
            shippingAddressRequired = null;
            request.getRequestDispatcher(SHIPPING_FORM).forward(request, response);
        }

    }
}
